package com.qingge.springboot.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qingge.springboot.entity.Paper;
import com.qingge.springboot.service.IPaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Resource
    private IPaperService paperService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //获取过期的key
        String expireKey = message.toString();
        //将考试状态设置成考试完成
        LambdaUpdateWrapper<Paper> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Paper::getId,expireKey).set(Paper::getIsStart,2);
        paperService.update(updateWrapper);
        //信息打印
        log.info("key : {} 失效" ,expireKey);
        //设备下线相关操作

    }

}